package com.zzq.aop;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzq.domain.paramet.Paramet;
import com.zzq.result.SmileResult;
import com.zzq.util.ObjectUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.List;

@Aspect
@Component
@EnableAspectJAutoProxy
public class PageHelperAspect {

    //数据库冯分页
    @Pointcut("@annotation(com.zzq.annotation.EnableMyPage)")
    public void serviceFindEnableMyPage(){}

    //逻辑分页
    /**
     * 使用around方法 在执行查询方法前执行PageHelper.startWith
     * 在执行查询方法后 将结果封装到PageInfo中
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("serviceFindEnableMyPage()")
    public Object serviceImplEnableMyPage(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        //获取连接点方法运行时的入参列表
        Object[] args = proceedingJoinPoint.getArgs();



        //获取连接点的方法签名对象
        Signature signature = proceedingJoinPoint.getSignature();
        Object target = proceedingJoinPoint.getTarget();
        //获取连接点所在的类的对象(实例)

        if (args[0] instanceof Paramet) {
            Paramet queryParameter = (Paramet) args[0];

            Integer page = queryParameter.getCurrentPage();
            Integer pageSize = queryParameter.getPageSize();
            if (ObjectUtils.isEmpty(page)||ObjectUtils.isEmpty(pageSize)){
                return proceedingJoinPoint.proceed();
            }
            PageHelper.startPage(page,pageSize);
        }

        //返回值
        Object object = proceedingJoinPoint.proceed();
//        log.info("方法[{}]执行结束.",signature.getName());

        return object;

    }
    @Around("execution(* com.zzq.controller.*.*(..))")
    public Object ControllerMethodExecution(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        Object object = proceedingJoinPoint.proceed();
        if (args.length==0) return object;
        if (args[0] instanceof Paramet) {
            Paramet queryParameter = (Paramet) args[0];
            Integer currentPage = queryParameter.getCurrentPage();
            Integer pageSize = queryParameter.getPageSize();
            if (!ObjectUtils.isEmpty(currentPage)&&!ObjectUtils.isEmpty(pageSize)) {
                if (object instanceof SmileResult) {
                    SmileResult result = (SmileResult) object;
                    Object value = result.getvalue();
                    if (value instanceof List) {
                        PageInfo<?> pageInfo = new PageInfo<>((List<?>) value);
                        if (ObjectUtils.isEmpty(pageInfo.getPages())) {
                            List list= (List) value;
                            int size = list.size();
                            int startIndex = (currentPage - 1) * pageSize;
                            int endIndex = Math.min(startIndex + pageSize,size);
                            result.setvalue(list.subList(startIndex,endIndex));
                            result.setTotal(Long.valueOf(size));
                        }else {
                            result.setTotal(pageInfo.getTotal());
                        }

                        result.setPageSize(pageSize);
                        result.setCurrentPage(currentPage);
                        return result;
                    }

                }
            }
        }
        return object;

    }

    private Integer getPageCount(int totalCount, int pageSize) {
        return (totalCount -1) / pageSize + 1;
    }

}
